package utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.parser.Feature;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @program: poiService
 * @description: 用来比较两个json是否相同
 * @create: 2020-07-15 18:36
 **/
public class JSONCompareUtil {

    /**
     * 把json拍平,生成JSONPath的形式，可以用JSONPath.eval获取到结果
     * $.result.fronts[3].name
     * @return
     */
    private static void flatJsonToString(List<String> result, String root, String jsonStr) {

        if(result == null) {
            throw new IllegalArgumentException("result is null");
        }

        if(!jsonStr.startsWith("[") && !jsonStr.startsWith("{")) {
            result.add(root + "=" + jsonStr);
            return;
        }

        Object jsonObject = JSONObject.parse(jsonStr, Feature.OrderedField);

        if(jsonObject instanceof JSONObject) {
            for (Map.Entry<String, Object> entry : ((JSONObject) jsonObject).entrySet()) {
                flatJsonToString(result,root + "." + entry.getKey(), entry.getValue().toString());
            }
        } else if( jsonObject instanceof JSONArray) {
            if(((JSONArray)jsonObject).size() == 0) {
                flatJsonToString(result,root, "");
            }

            JSONArray array = (JSONArray)jsonObject;
            for(int i=0; i<array.size(); i++) {
                flatJsonToString(result, root + "[" + i + "]", array.get(i).toString());
            }
        }
    }

    /**
     * 获取一个排序好的结果
     * @param jsonStr
     * @return
     */
    private static List<String> flatJsonToString(String jsonStr) {
        List<String> result = new ArrayList<>();
        flatJsonToString(result, "$", jsonStr);
        result.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        return result;
    }

    /**
     * 比较两个json是否相同
     * @param jsonStr1
     * @param jsonStr2
     * @return
     */
    public static boolean compareJson(String jsonStr1, String jsonStr2) {
       List<String> result1 = flatJsonToString(jsonStr1);
       List<String> result2 = flatJsonToString(jsonStr2);

        if(result1.size() != result2.size()) {
            return false;
        }

        //挨个比较每个元素是否相同
        for(int i=0; i<result1.size(); i++) {
            if(!result1.get(i).equals(result2.get(i))) {
                System.out.println(jsonStr1);
                System.out.println(jsonStr2);
                return false;
            }
        }

        return true;
    }



    public static void main(String[] args) {
//        List<String> result = new ArrayList<>();
//        new JSONCompareUtil().flatJsonToString(result, "$", jsonStr2);
//        result.stream().forEach(item ->{
//            System.out.println(item);
//        });
//        System.out.println(new JSONCompareUtil().compareJson(jsonStr2, jsonStr2));
        System.out.println(JSONPath.eval(jsonStr2,"$.result.fronts[3].name"));

        new JSONCompareUtil().compareJson(jsonStr, jsonStr2);
    }

    private static String jsonStr = "";
    private static String jsonStr2 = "";
}
